//package com.yonyou.gmmc.service.shopping.eventhandler;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import org.marketing.common.dto.EventDataDTO;
//import org.marketing.common.event.BizEvent;
//import org.marketing.common.event.EventShoppingJDDTO;
//import org.marketing.common.event.EventShoppingPointsDTO;
//import org.marketing.common.event.EventShoppingToPayDTO;
//import org.marketing.common.event.enums.EventConstant;
//import org.marketing.common.event.enums.EventConstant.EventBizStep;
//import org.marketing.common.event.enums.EventConstant.EventBizType;
//import org.marketing.common.util.MapConverUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import com.yonyou.gmmc.common.eventhandler.AbstractEventHandler;
//import com.yonyou.gmmc.service.shopping.constant.ShoppingConstant;
//import com.yonyou.gmmc.service.shopping.model.TmTpOrderInfo;
//import com.yonyou.gmmc.service.shopping.service.ShoppingOrderService;
//import com.yonyou.gmmc.service.shopping.service.ShoppingServiceInterface;
//import com.yonyou.gmmc.service.shopping.util.SpringUtil;
//
///**
// * 订单的积分扣减事件
// * 对应的事件号为10081002
// * @author BENJAMIN
// *
// */
//@Component("10081002")
//public class ShoppingPointsChanged extends AbstractEventHandler{
//	
//	private static Logger logger = LoggerFactory.getLogger(ShoppingPointsChanged.class);
//	
//	@Autowired
//	private ShoppingOrderService     shoppingOrderService;
//	
//	@Autowired
//	private ShoppingServiceInterface shoppingServiceInterface;
//	
//	// 商城下单
//	private static EventBizType BizType = EventConstant.EventBizType.SHOPPIING_ORDER;
//	// 积分扣减变化
//	private static EventBizStep BizStep = EventConstant.EventBizStep.SHOPPIING_ORDER_POINTS;
//
//	@Override
//	protected String getHandleInfo() {
//		// TODO Auto-generated method stub
//		return "处理订单的积分扣减事件";
//	}
//
//	@Override
//	protected EventDataDTO preTransform(BizEvent event) throws Exception {
//		EventShoppingPointsDTO o = new EventShoppingPointsDTO();
//		try {
//			o = (EventShoppingPointsDTO) MapConverUtil.map2POStrict(event.getEventData(), o);
//			logger.info(o.toString());
//		} catch (Exception e) {
//			logger.error(e.getMessage(), e);
//			this.backInfo = "eventData中的业务数据不能识别：" + e.getMessage();
//			throw new Exception("eventData中的业务数据不能识别"+e.getMessage());
//		}
//		return o;
//	}
//
//	@Override
//	protected boolean handle(EventDataDTO data)  throws Exception{
//		EventShoppingPointsDTO thisEventData = (EventShoppingPointsDTO)data;
//		boolean isPay = thisEventData.isPay();
//		boolean isOrder = false;//是否向JD下单成功
//		if(isPay){	//积分扣减成功
//			logger.info("积分扣减成功-----");
//			
//			try {
//				// 1.更新订单的对应的状态为支付成功
//				logger.info("开始更新订单的状态------");
//				//TODO
//				TmTpOrderInfo orderInfo = new TmTpOrderInfo();
//				orderInfo.setOrderStatus(ShoppingConstant.ORDER_STATUS_POINTS_DEDUCTED);
//				logger.info("状态为——"+orderInfo.getOrderStatus());
//				orderInfo.setThirdOrder(thisEventData.getShoppingNo());
//				logger.info("订单号为——"+orderInfo.getThirdOrder());
//				String result = shoppingOrderService.updateOrderStatus(orderInfo);
//				logger.info("更新结果："+result);
//				
//				// 2.调用JD的下单接口 
//				// 注意：JD的接口调用失败，需要发一个JD京东接口失败的事件
//				//		JD的接口调用成，直接更新订单状态为先成功
//				logger.info("开始调用JD的下单接口------");
//				//TODO
//				ShoppingOrderService shoppingService=(ShoppingOrderService)SpringUtil.getBean("shoppingOrderService");
//				Map<String, String> resultMap = new HashMap<String, String>();
//				resultMap = shoppingOrderService.submitJDOrder(thisEventData.getShoppingNo(),thisEventData.getUserId());
//				String code = resultMap.get("resultCode");
//				if(code.equals(ShoppingConstant.ORDER_STATUS_SUCCESS)){
//				    //京东下单成功
//				    logger.info("成功返回结果------"+resultMap.get("resultMsg"));
//	                isOrder=true;
//				}else{
//				    //京东下单失败
//                    logger.info("失败返回结果------"+resultMap.get("resultMsg"));
//                    //发送京东下单接口失败事件
//                    //TODO
//                    logger.info("JD京东接口失败————");
//                    isOrder=false;
////                    EventShoppingJDDTO shoppingJDEventDto = new EventShoppingJDDTO();
////                    shoppingJDEventDto.setOrdered(isOrder);
////                    logger.info("jd 是否下单成功——"+shoppingJDEventDto.isOrdered());
////                    shoppingJDEventDto.setPointNums(thisEventData.getChangeNum());
////                    logger.info("积分数量——"+shoppingJDEventDto.getPointNums());
////                    shoppingJDEventDto.setShoppingOrderNo(thisEventData.getShoppingNo());
////                    logger.info("订单号——"+shoppingJDEventDto.getShoppingOrderNo());
////                    shoppingJDEventDto.setUserId(Integer.valueOf(thisEventData.getUserId()));
////                    logger.info("下单的用户id——"+shoppingJDEventDto.getUserId());
////                    BizEvent event = new BizEvent();
////                    event.setEventBizType(EventBizType.SHOPPIING_ORDER);
////                    event.setEventBizStep(EventBizStep.SHOPPIING_ORDER_JD);
////                    event.setEventKey(shoppingJDEventDto.getShoppingOrderNo()+"Fail");
////                    event.setEventData(MapConverUtil.PO2Map(shoppingJDEventDto));
////                    shoppingOrderService.sendEvent2MQ(event);
////                    logger.info("JD京东接口失败的事件————End");
//				}
//			} catch (Exception e) {
//				isOrder=false;
//				this.backInfo="系统异常，后续下单更新失败,原因"+e.getMessage();
//				logger.error("订单状态更新失败------",e);
//				
//			}
//		     //订单更新结束之后发送消息到MQ
//	        EventShoppingJDDTO eventData = new EventShoppingJDDTO( Integer.parseInt(thisEventData.getUserId()), thisEventData.getShoppingNo(), isOrder,thisEventData.getChangeNum());
//	        BizEvent event = new BizEvent("",EventBizType.SHOPPIING_ORDER, EventBizStep.SHOPPIING_ORDER_JD, MapConverUtil.PO2Map(eventData));
//	        shoppingOrderService.sendEvent2MQ(event);
//		}else{
//			
//			//1.更新订单的状态，状态更新为下单失败，原因从event中取reason 流程结束
//			try {
//				logger.info("积分扣减失败------订单状态改成下单失败");
//				//TODO
//                TmTpOrderInfo orderInfo = new TmTpOrderInfo();
//                orderInfo.setOrderStatus(ShoppingConstant.ORDER_STATUS_FIALED);
//                logger.info("状态为——"+orderInfo.getOrderStatus());
//                orderInfo.setThirdOrder(thisEventData.getShoppingNo());
//                logger.info("订单号为——"+orderInfo.getThirdOrder());
//                String result = shoppingOrderService.updateOrderStatus(orderInfo);
//                logger.info("更新结果："+result);
//				isOrder=true;
//			} catch (Exception e) {
//				isOrder=false;
//				this.backInfo="系统异常，订单状态更新失败,原因"+e.getMessage();
//				logger.error("订单状态更新失败------",e);
//			}
//			
//		}
//
//		
//		return isOrder;
//	}
//
//	/*
//	 * 判断是否重复的方法
//	 */
//	@Override
//	protected boolean preCheck(BizEvent event) throws Exception {
//		if(shoppingOrderService.queryEventNum(event)>0){
//			this.backInfo="该消息已经处理，消息key="+event.getEventKey();
//			return false;
//		}
//		return true;
//	}
//
//	/* 
//	 * 事件处理完成后要做的事情
//	 * 
//	 */
//	@Override
//	protected void afterPay(BizEvent event) {
//		shoppingOrderService.insertHandleLog(event);
//	}
//	
//
//}
